<?php
/**
 * 新医嘱提醒操作上传
 * @authors xiadx (xiadx@zpdchina.com)
 * @date    2017-03-08 19:32:00
 * @version $Id$
 */

class TixingdatauploadController extends Yaf_Controller_Abstract {

    public function indexAction()
    {
    	$request = $this->getRequest();
    	$user_department_id = $request->getPost('user_department_id', '');
    	$compression_http_data = $request->getPost('compression_http_data', 'off');
    	$upload_data = $request->getPost('data', '');
        $msg = "";
    	$data = Utility::uncompress($upload_data, $compression_http_data);
    	if (!$data) {
    		Utility::apiJsonError('10010', '请上传数据!');
    	}
        $yiyuan_id = $request->getPost("yiyuan_id","");
        if($yiyuan_id === "" )
        {
            Utility::apiJsonError(20028, '请输入医院ID');
        }
        elseif(!ctype_digit("$yiyuan_id"))
        {
            Utility::apiJsonError(20027, '医院ID必须是整型');
        }
//        $arr[]  = array(
//            'type'=>"临时",
//            'zhixing_zuhao'=>"370882_1_1_0",
//            'zhixing_hushi_id'=>"012",
//            'zhixing_hushi_name'=>"于莹梅",
//            'zhixing_time'=>"2017-3-16 10:52:56",
//            'zhuyuan_id'=>"370882",
//
//        );
       // $str = json_encode($arr);
    	$data = json_decode($data, true);

    	/*$values = [
    		[ 'zhuyuan_id' => '12121212', 'fare_level' => '1', 'temperature' => '35', 'jiancha_time' => '2016-07-28 10:00', 'patient_level' => 2, 'is_fara' => 1,'yiyuan_id' => 0],
    		[ 'zhuyuan_id' => '12121212', 'fare_level' => '1', 'temperature' => '35', 'jiancha_time' => '2016-07-28 10:00', 'patient_level' => 2, 'is_fara' => 1,'yiyuan_id' => 0],
    	];*/

    	if (empty($data)) {
    		Utility::apiJsonError('10010', '数据为空或者数据格式不正确');
    	}else{

            foreach ($data as $key=>$item)
            {
                if(in_array("", $item)||count($item) != 6)
                {
                    if($msg == "")
                    {
                        $msg .= $item['zhixing_zuhao'];
                    }else{
                        $msg .= ",".$item['zhixing_zuhao'];
                    }
                    unset($data[$key]);
                    continue;
                }
                $data[$key]['yiyuan_id'] = $yiyuan_id;
                $data[$key]['state'] = isset($data[$key]['state']) ? $data[$key]['state'] : 0;

            }
        }
    	$zuhao_arr = Utility::array_pluck($data, 'zhixing_zuhao');

    	if (!empty($zuhao_arr)) {
    		$affect_rows =  DB::update('zhuyuan_yizhu_tixing_info')
                ->set(array("is_remind"=>1))
                ->where('zuhao', 'IN', $zuhao_arr)
                ->execute();
    	}
    	if ($affect_rows) {
    		list($insert_id, $rows) = Utility::multiInsert('zhuyuan_yizhu_tixing_histroy',$data);
    	}
    	$result = ['error' => 200, 'msg' => $msg];
    	Utility::apiJsonResultExtends($result);
    }
}
